package authentication.token.client;

import authentication.token.Request;
import authentication.token.Response;
import authentication.token.server.LoginController;
import authentication.token.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static authentication.token.SignUtil.*;

/**
 * @author yuqi<307530884@qq.com> <2017/3/28>
 * @version 1.0
 * @since 1.0
 */
public class Client {

    private transient final Logger logger = LoggerFactory.getLogger(getClass());

    private String trueToken;

    public Client doLoginIn(){

        Request request = new Request();
        request.put(Request.url, LoginController.LOGIN_URL);
        request.put("userName", "yuqi");
        request.put("pwd", "123");

        String key = setLoginSign(request);

        Response response = Server.doSend(request);
        logger.debug("doLoginIn get response:{}", response);
        if("1".equals(response.get("code"))){
            this.trueToken = getToken(response, key);
            logger.debug("doLoginIn get trueToken:{}", trueToken);
        }

        return this;
    }

    public Client doGetInfo(){
        Request request = new Request();
        request.put(Request.url, LoginController.GET_LIST_URL);

        Response response = Server.doSend(request);

        logger.debug("doGetInfo get response:{}",response);

        return this;
    }

    public Client doError(){
        Request request = new Request();

        Response response = Server.doSend(request);


        logger.debug("doError get response:{}",response);

        return this;
    }

    public static void main(String[] args) {
//        new Client().doGetInfo().doLoginIn().doError();
        new Client().doLoginIn().doGetInfo();
    }

}
